package cn.flkj.evidence.RocketMQConsumer;

import cn.flkj.evidence.EvidenceImpl;
import cn.flkj.evidence.repositories.EvidenceMapper;
import cn.flkj.evidence.sample.EvidenceData;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.security.SignatureException;


@Slf4j
@Component
public class RocketMQToEvidence {

    private static final String USER = "user.jks";
    private static final String ARBITRATOR = "arbitrator.jks";
    private static final String DEPOSITOR = "depositor.jks";
    private static final String UNIVERSAL_PASSWORD = "123456"; //通用密码，防止写错
    private static final String EVIDENCE_SIGNERS_ADDRESS = "0x0f74e2529e561b2749034a3d235da8cf04ab8bea";  // 工厂合约地址

/*    @Autowired
    private EvidenceImpl evidenceImpl;
    @Resource
    private EvidenceMapper evidenceMapper;
    public RocketMQToEvidence(EvidenceMapper evidenceMapper ,EvidenceImpl evidenceImpl){
        this.evidenceMapper = evidenceMapper;
        this.evidenceImpl = evidenceImpl;
    }*/

    public JSONObject send(String md5Hash, EvidenceImpl evidenceImpl, EvidenceMapper evidenceMapper) {
        //1. 上链
        JSONObject result = new JSONObject();
        String newEvidenceAddress = null;
        int max_id = evidenceMapper.evidenceIdMax();
        try {
            newEvidenceAddress = evidenceImpl.newEvidence(
                    USER,
                    UNIVERSAL_PASSWORD,
                    UNIVERSAL_PASSWORD,
                    EVIDENCE_SIGNERS_ADDRESS,
                    String.valueOf(max_id + 1),
                    md5Hash).getValue();
        } catch (Exception e) {
            log.error("存证上链错误： " + e);
        }

        //2. 签名，多方签名
        EvidenceData evidenceData = null;
        try {
            evidenceData = evidenceImpl.getEvidence(
                    USER,
                    UNIVERSAL_PASSWORD,
                    UNIVERSAL_PASSWORD,
                    newEvidenceAddress);
        } catch (Exception e) {
            log.error("签名错误： " + e);
        }
        boolean flag = false, flag1 = false, traceSourceFlag = false;
        String evidenceHash = evidenceData.getEvidenceHash();
        try {
            flag = evidenceImpl.sendSignatureToBlockChain(
                    ARBITRATOR,
                    UNIVERSAL_PASSWORD,
                    UNIVERSAL_PASSWORD,
                    newEvidenceAddress,
                    evidenceHash
            );
            flag1 = evidenceImpl.sendSignatureToBlockChain(
                    DEPOSITOR,
                    UNIVERSAL_PASSWORD,
                    UNIVERSAL_PASSWORD,
                    newEvidenceAddress,
                    evidenceHash
            );
        } catch (Exception e) {
            log.error("签名错误： " + e.getMessage());
//            log.error(e.getMessage());
//            log.error(e.toString());
//            log.error(e.getLocalizedMessage());
        }
        if (flag == true && flag1 == true) {
            log.info("多方签名成功");
        }

        evidenceMapper.insert(
                md5Hash,
                "zhangshulin",
                "text1.txt",
                newEvidenceAddress,
                1
        );

        //3. 溯源
        EvidenceData evidenceData1 = null;
        try {
            evidenceData1 = evidenceImpl.getEvidence(
                    USER, UNIVERSAL_PASSWORD, UNIVERSAL_PASSWORD,
                    newEvidenceAddress);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            traceSourceFlag = evidenceImpl.verifyEvidence(evidenceData1);
        } catch (SignatureException e) {
            e.printStackTrace();
        }
        if (traceSourceFlag == true) {
            log.info("success");
            result.put("code", 200);
            result.put("evidenceAddress", newEvidenceAddress);
        }
        return result;

    }


}
